package test.common.api;
//package test.exam;
//
//import java.awt.image.BufferedImage;
//import java.io.Closeable;
//import java.io.IOException;
//import java.io.OutputStream;
//import java.io.OutputStreamWriter;
//
//import javax.imageio.ImageIO;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//
//import com.github.napp.kaptcha.Kaptcha;
//import com.github.napp.kaptcha.KaptchaTextCreator;
//
///**
// * Servlet implementation class KaptchaServlet
// */
//public class ExamKaptchaServlet extends HttpServlet {
//    private static final long serialVersionUID = 1L;
//
//    private static final String ATTRIBUTE_CAPTCHA = "captcha";
//
//    private static final String PARAMETER_GUESS = "guess";
//    private static final String PARAMETER_ACTION = "action";
//
//    private static final String ACTION_DISPLAY = "display";
//    private static final String ACTION_NEW_TEXT = "new";
//    private static final String ACTION_CHECK = "check";
//
//    private static final int DEFAULT_KAPTCHA_LEN = 7;
//
//    private final Kaptcha kaptcha = new Kaptcha();
//
//    /**
//     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
//     */
//    @Override
//    protected final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
//        doPost(request, response);
//    }
//
//    /**
//     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
//     */
//    @Override
//    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
//        final HttpSession session = request.getSession();
//        final KaptchaCache cache = getKaptchaCache(session);
//        final String actionParameter = request.getParameter(PARAMETER_ACTION);
//        if (ACTION_DISPLAY.equals(actionParameter)) {
//            attachImageToResponse(cache.image, response);
//            return;
//        }
//        if (ACTION_NEW_TEXT.equals(actionParameter)) {
//            createAndAddKaptcha(session);
//        }
//        OutputStreamWriter out = null;
//        response.setContentType("text/html");
//        try {
//            out = new OutputStreamWriter(response.getOutputStream());
//
//            if (ACTION_CHECK.equals(actionParameter)) {
//                final String guess = request.getParameter(PARAMETER_GUESS);
//                final boolean correct = cache.actualText.equals(guess);
//                if (!correct) {
//                    out.append("You guessed " + guess + " but it was " + cache.actualText + "<br><br>");
//                } else {
//                    out.append("Correct, next one!<br><br>");
//                    createAndAddKaptcha(session);
//                }
//            }
//            out.append("<img src='?action=" + ACTION_DISPLAY + "'/>");
//            out.append("<form action='?action=" + ACTION_CHECK + "' method='post'>");
//            out.append("<input type='text' name='" + PARAMETER_GUESS + "'><input type='submit'><br>");
//            out.append("</form>");
//
//            out.flush();
//        } catch (final IOException ex) {
//            close(out);
//        }
//    }
//
//    /**
//     * @param image
//     * @param response
//     * @throws IOException
//     */
//    private static void attachImageToResponse(final BufferedImage image, final HttpServletResponse response) throws IOException {
////      this may cause problems depending on the environment, best to disable it
//        if (ImageIO.getUseCache()) {
//            ImageIO.setUseCache(false);
//        }
//        OutputStream out = null;
//        try {
//            response.setHeader("Cache-Control", "no-store, no-cache");
//            response.setContentType("image/png");
//            out = response.getOutputStream();
//            ImageIO.write(image, "png", out);
//        } finally {
//            close(out);
//        }
//    }
//
//    /**
//     * @param out
//     */
//    private static void close(final Closeable out) {
//        if (null != out) {
//            try {
//                out.close();
//            } catch (final IOException e) {
//                e.printStackTrace(); // might want to log this
//            }
//        }
//    }
//
//    /**
//     * @param session
//     * @return
//     */
//    private KaptchaCache getKaptchaCache(final HttpSession session) {
//        final Object attribute = session.getAttribute(ATTRIBUTE_CAPTCHA);
//        if (attribute instanceof KaptchaCache) {
//            return (KaptchaCache) attribute;
//        }
//        return createAndAddKaptcha(session);
//    }
//
//    /**
//     * @param session
//     * @return
//     */
//    private KaptchaCache createAndAddKaptcha(final HttpSession session) {
//        final KaptchaCache kaptchaCache = new KaptchaCache();
//        session.setAttribute(ATTRIBUTE_CAPTCHA, kaptchaCache);
//        return kaptchaCache;
//    }
//
//    /**
//     * @author abledea
//     * @since Oct 26, 2014
//     */
//    private final class KaptchaCache {
//        final String actualText = KaptchaTextCreator.getText(DEFAULT_KAPTCHA_LEN);
//        final BufferedImage image = kaptcha.createImage(actualText);
//    }
//}